package cn.tude.egweibo4.controller;


import cn.tude.egweibo4.base.response.JsonResult;
import cn.tude.egweibo4.base.response.StatusCode;
import cn.tude.egweibo4.mapper.WeiboMapper;
import cn.tude.egweibo4.pojo.entity.User;
import cn.tude.egweibo4.pojo.entity.Weibo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("v1/weibo/")
@Api(tags = "02.微博模块")
public class WeiboController {
    @Autowired(required = false)
    private WeiboMapper weiboMapper;


    @PostMapping("insert")
    @ApiOperation(value = "发布微博")
    public JsonResult insertWeibo(@RequestBody String content, @ApiIgnore HttpSession session){
        Weibo weibo=new Weibo();
        weibo.setContent(content);
        User user=(User) session.getAttribute("user");
        if (user==null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        weibo.setUserId(user.getId());
        int i = weiboMapper.insertWeibo(weibo);
        if (i==1)
            return JsonResult.ok();
        return JsonResult.notok();
    }


    @GetMapping("selectIndex")
    @ApiOperation(value = "微博首页")
    public JsonResult selectWeiboIndex(){
        return JsonResult.ok(weiboMapper.selectWeiboIndex());
    }


    @GetMapping("selectById")
    @ApiOperation(value = "微博详情页")
    @ApiImplicitParam(name = "id",value = "微博ID",required = true)
    public JsonResult selectById(Long id){
        return JsonResult.ok(weiboMapper.selectByWeiboId(id));
    }
}
